<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../common/Logger.php");
require_once(dirname(__FILE__) . "/../common/Common.php");
require_once(dirname(__FILE__) . "/../dbcom/AppointCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ReservetimeCom.php");
require_once(dirname(__FILE__) . "/../dbcom/RagionCom.php");
require_once(dirname(__FILE__) . "/../dbcom/UsertypeCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ServiceCom.php");
require_once(dirname(__FILE__) . "/../dbcom/StaisCom.php");

$common = new Common();
$appointcom = new AppointCom();
$reservetimecom = new ReservetimeCom();
$ragioncom = new RagionCom();
$usertypecom = new UsertypeCom();
$servicecom = new ServiceCom();
$staiscom = new StaisCom();

if (!isset($_REQUEST['cont']) || empty($_REQUEST['cont'])) {
    echo json_encode(array("code" => 100, "msg" => "cannot find cont"));
    exit();
}
$cont = $_REQUEST['cont'];
switch ($cont) {
    case 1://预约列表\
        if (!isset($_REQUEST['userid']) || empty($_REQUEST['userid'])) {
            echo json_encode(array("code" => 100, "msg" => "cannot find userid"));
            exit();
        }
        $userid = $_REQUEST['userid'];
        $res = $appointcom->getAppointlist($userid);
        if (!$res) {
            echo json_encode(array("code" => 400, "msg" => "get appointlist fail"));
            exit();
        }
        $result = array("code" => 0, "msg" => "success", "data" => array());
        if (!empty($res)) {
            foreach ($res as $k => $v) {
                $aa = $reservetimecom->getRtimeById($v['reservetime_id']);
                $time = date("m-d", $v['time']);
                $result["data"][$k]["reservetime"] = $time . " " . $aa['info'];
                $result["data"][$k]["address"] = $v['address'];
                switch ($v['status']) {
                    case 1:
                        $result["data"][$k]["status"] = "预约成功";
                        break;
                    case 2:
                        $result["data"][$k]["status"] = "预约取消";
                        break;
                    default :
                        $result["data"][$k]["status"] = "等待工作人员确认";
                        break;
                }
                $result["data"][$k]["id"] = $v['id'];
            }
        }
        echo json_encode($result);
        break;
    case 2://预约详情
        if (!isset($_REQUEST['appointid']) || empty($_REQUEST['appointid'])) {
            echo json_encode(array("code" => 100, "msg" => "cannot find appointid"));
            exit();
        }
        $appointid = $_REQUEST['appointid'];
        $appoint = $appointcom->getAppointById($appointid);
        if (!$appoint) {
            echo json_encode(array("code" => 400, "msg" => "get appoint fail"));
            exit();
        }
        $result = array("code" => 0, "msg" => "success", "data" => array());
        switch ($appoint['status']) {
            case 1:
                $result["data"]["status"] = "预约成功";
                break;
            case 2:
                $result["data"]["status"] = "预约取消";
                break;
            default :
                $result["data"]["status"] = "等待工作人员确认";
                break;
        }
        $result["data"]["statuscode"] = $appoint['status'];
        $aa = $reservetimecom->getRtimeById($appoint['reservetime_id']);
        $time = date("m-d", $appoint['time']);
        $result["data"]["reservetime"] = $time . " " . $aa['info'];
        $result["data"]["address"] = $appoint['address'];
        $usertype = $usertypecom->getUsertypeById($appoint['usertype_id']);
        $result["data"]["usertype"] = $usertype['name'];
        $area = $ragioncom->getRagionById($appoint['area_id']);
        $result["data"]["area"] = $area['name'];
        $street = $ragioncom->getRagionById($appoint['street_id']);
        $result["data"]["street"] = $street['name'];
        $community = $ragioncom->getRagionById($appoint['community_id']);
        $result["data"]["community"] = $community['name'];
        $result["data"]["username"] = $appoint['username'];
        if ($appoint['sex'] == 1) {
            $result["data"]["sex"] = "男";
        } else {
            $result["data"]["sex"] = "女";
        }
        $result["data"]["codeid"] = $appoint['codeid'];
        $result["data"]["mobile"] = $appoint['mobile'];
        //$result["data"]["stais"] = $appoint['stais_id'];
        if(!empty($appoint['stais_id'])){
            $stais = $staiscom->getStaisById($appoint['stais_id']);
            $result["data"]["stades"] = $stais['des'];
            switch ($stais['satisfied']){
                case 1:
                    $result["data"]["stais"] = "基本满意";
                    break;
                case 2:
                    $result["data"]["stais"] = "不满意";
                    break;
                default :
                    $result["data"]["stais"] = "满意";
                    break;
            }
        }else{
            $result["data"]["stais"] = 0;
        }
        $result["data"]["id"] = $appoint['id'];
        echo json_encode($result);
        break;
    default :
        echo json_encode(array("code" => 500, "msg" => "undefined error"));
        break;
}
exit();
